package com.blockchain.securewallet.view;

import android.content.Context;
import android.graphics.ColorMatrixColorFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

/**
 * Created by Chain-Cloud on 2018/5/16.
 */

public class SelectorImageView extends ImageButton {

    public SelectorImageView(Context context) {
        super(context);
        this.setOnTouchListener(VIEW_TOUCH_DARK);
    }
    public SelectorImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOnTouchListener(VIEW_TOUCH_DARK);
    }
    public View.OnTouchListener VIEW_TOUCH_DARK = new OnTouchListener() {
        //变暗(三个-50，值越大则效果越深)
        public final float[] BT_SELECTED_DARK = new float[] { 1, 0, 0, 0, -50, 0, 1,
                0, 0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0 };

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                ImageView iv = (ImageView) v;
                iv.setColorFilter(new ColorMatrixColorFilter(BT_SELECTED_DARK));
                iv.setAlpha(0.1f);
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                ImageView iv = (ImageView) v;
                iv.clearColorFilter();
                mPerformClick();
                iv.setAlpha(1.0f);
            } else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
                ImageView iv = (ImageView) v;
                iv.clearColorFilter();
                iv.setAlpha(1.0f);
            }
            return true;  //如为false，执行ACTION_DOWN后不再往下执行
        }
    };

    private void mPerformClick(){
        SelectorImageView.this.performClick();
    }
}
